/**
 * 
 */
package com.nari.heatassist.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.nari.baseinfo.dao.IBuildingDao;
import com.nari.baseinfo.dao.ICommunityDao;
import com.nari.baseinfo.dao.IHeatMeterDao;
import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.IHeatMeter;
import com.nari.coreframe.service.GenericServiceImpl;
import com.nari.heatassist.dao.IBuildingConsumeConfigDao;
import com.nari.heatassist.model.IBuildingConsumeConfig;
import com.nari.heatassist.utils.BuildingConsumeConfigEntity;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;

/**
 * 
 * @author Xiaopeng
 * @date 2014-4-22
 */
public class BuildingConsumeConfigServiceImpl extends GenericServiceImpl<IBuildingConsumeConfig, String>
		implements IBuildingConsumeConfigService {
	private ICommunityDao iCommunityDao;
	private IBuildingDao iBuildingDao;
	private IHeatMeterDao iHeatMeterDao;

	/**
	 * 分页查询楼栋耗热配置项
	 * @param projectUID
	 * @param communityUID
	 * @param buildingUID
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-23
	 */
	public ResultWithPageCond<BuildingConsumeConfigEntity> queryConsumeConfigPage(String projectUID, 
			String communityUID, String buildingUID, PageCond pageCond) throws Exception {
		IBuildingConsumeConfigDao iBuildingConsumeConfigDao = (IBuildingConsumeConfigDao) this.getiGenericDao();
		return iBuildingConsumeConfigDao.queryConsumeConfigPage(projectUID, communityUID, buildingUID, pageCond);
	}
	
	/**
	 * 通过楼栋耗热配置查询与之关联的小区ID-小区名称映射表
	 * @param configList
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-23
	 */
	public Map<String, String> queryReferCommunitysByConfigList(List<BuildingConsumeConfigEntity> configList) throws Exception {
		List<String> communityIDList = null;
		if(configList!=null && configList.size()>0) {
			communityIDList = new ArrayList<String>();
			for(BuildingConsumeConfigEntity configEntity: configList) {
				communityIDList.add(configEntity.getCommunityUID());
			}
		}
		return iCommunityDao.queryIdNameMapByIDList(communityIDList);
	}
	
	/**
	 * 通过楼栋耗热配置查询与之关联的楼栋ID-楼栋名称映射表
	 * @param configList
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-23
	 */
	public Map<String, String> queryReferBuildingsByConfigList(List<BuildingConsumeConfigEntity> configList) throws Exception {
		List<String> buildingIDList = null;
		if(configList!=null && configList.size()>0) {
			buildingIDList = new ArrayList<String>();
			for(BuildingConsumeConfigEntity configEntity: configList) {
				buildingIDList.add(configEntity.getBuildingUID());
			}
		}
		return iBuildingDao.queryIdNameMapByIDList(buildingIDList);
	}
	
	/**
	 * 通过楼栋耗热配置查询与之关联的楼栋ID-楼栋名称映射表
	 * @param configList
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-23
	 */
	public Map<String, String> queryReferBuildingsByMeasureConfigList(List<IBuildingConsumeConfig> configList) throws Exception {
		List<String> buildingIDList = null;
		if(configList!=null && configList.size()>0) {
			buildingIDList = new ArrayList<String>();
			for(IBuildingConsumeConfig config: configList) {
				buildingIDList.add(config.getBuildingID());
			}
		}
		return iBuildingDao.queryIdNameMapByIDList(buildingIDList);
	}
	
	/**
	 * 分页查询参与楼栋热计量的配置列表
	 * @param buildingUID
	 * @param meterNo
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-23
	 */
	public ResultWithPageCond<IBuildingConsumeConfig> queryMeasureMeterPage(String buildingUID, String meterNo, 
			PageCond pageCond) throws Exception {
		IBuildingConsumeConfigDao iBuildingConsumeConfigDao = (IBuildingConsumeConfigDao) this.getiGenericDao();
		return iBuildingConsumeConfigDao.queryMeasureMeterPage(buildingUID, meterNo, pageCond);
	}
	
	/**
	 * 通过楼栋信息删除楼栋耗热计量配置项
	 * @param buildingUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-23
	 */
	public ResponseMsg deleteBuildingConsumeConfig(String buildingUID) throws Exception {
		IBuildingConsumeConfigDao iBuildingConsumeConfigDao = (IBuildingConsumeConfigDao) this.getiGenericDao();
		return iBuildingConsumeConfigDao.deleteBuildingConsumeConfig(buildingUID);
	}
	
	/**
	 * 查询可用于楼栋耗热配置的热表列表，剔掉已参与该楼栋配置的热量表
	 * 根据楼栋与热表的物理关系(主干、分支等关系)，只要热表与楼栋在同一小区，即可进行逻辑关系配置
	 * @param projectUID
	 * @param communityUID
	 * @param meterNo
	 * @param buildingUID
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-24
	 */
	public ResultWithPageCond<IHeatMeter> queryConfigMeasureMeterPage(String projectUID, 
			String communityUID, String meterNo, String buildingUID, 
			PageCond pageCond) throws Exception {
		IBuildingConsumeConfigDao iBuildingConsumeConfigDao = (IBuildingConsumeConfigDao) this.getiGenericDao();
		return iBuildingConsumeConfigDao.queryConfigMeasureMeterPage(projectUID, communityUID, meterNo, buildingUID, pageCond);
	}
	
	/**
	 * 保存楼栋耗热计量的热量表关系配置
	 * @param buildingUID
	 * @param meterOptions 热量表选项及其设置信息
	 * 	格式(热量表ID:热量表加减关系)：XXX:XXX,XXX:XXX,XXX:XXX
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-24
	 */
	public ResponseMsg saveOrUpdateBuildingMeasureMeterConfig(String buildingUID, 
			String meterOptions) throws Exception {
		if(meterOptions!=null && !"".equals(meterOptions)) {
			IBuilding building = iBuildingDao.queryEntityById(buildingUID);
			IBuildingConsumeConfigDao iBuildingConsumeConfigDao = (IBuildingConsumeConfigDao) this.getiGenericDao();
			String[] meterOptionArray = meterOptions.split("[,]");
			for(String meterOption: meterOptionArray) {
				String[] meterInfo = meterOption.split("[:]");
				//热表查询
				IHeatMeter heatMeter = iHeatMeterDao.queryEntityById(meterInfo[0]);
				//查询配置项
				IBuildingConsumeConfig config = iBuildingConsumeConfigDao.queryConfigByRelation(buildingUID, heatMeter.getMeterNo());
				if(config == null) { 
					config = new IBuildingConsumeConfig();
					config.setBuildingID(buildingUID);
					config.setMeterNo(heatMeter.getMeterNo());
				} 
				//楼栋信息
				if(building != null) {
					config.setProjectID(building.getProjectID());
					config.setCommunityID(building.getCommunityID());
					config.setBuildingName(building.getBuildingName());
				}
				//设置热量表计算关系
				config.setCalculateType(meterInfo[1]);
				this.getiGenericDao().saveOrUpdateEntity(config);
			}
			return ResponseMsg.getInstance(true, "");
		}
		return ResponseMsg.getInstance(false, "请填写完整的的楼栋、热量表配置关系");
	}
	
	/**
	 * 通过楼栋与热量表的关联关系删除配置项
	 * @param buildingUID
	 * @param meterNo
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-24
	 */
	public ResponseMsg deleteConfigByRelation(String buildingUID, 
			String meterNo) throws Exception {
		IBuildingConsumeConfigDao iBuildingConsumeConfigDao = (IBuildingConsumeConfigDao) this.getiGenericDao();
		return iBuildingConsumeConfigDao.deleteConfigByRelation(buildingUID, meterNo);
	}

	/**
	 * @return the iCommunityDao
	 */
	public ICommunityDao getiCommunityDao() {
		return iCommunityDao;
	}

	/**
	 * @param iCommunityDao the iCommunityDao to set
	 */
	public void setiCommunityDao(ICommunityDao iCommunityDao) {
		this.iCommunityDao = iCommunityDao;
	}

	/**
	 * @return the iBuildingDao
	 */
	public IBuildingDao getiBuildingDao() {
		return iBuildingDao;
	}

	/**
	 * @param iBuildingDao the iBuildingDao to set
	 */
	public void setiBuildingDao(IBuildingDao iBuildingDao) {
		this.iBuildingDao = iBuildingDao;
	}

	/**
	 * @return the iHeatMeterDao
	 */
	public IHeatMeterDao getiHeatMeterDao() {
		return iHeatMeterDao;
	}

	/**
	 * @param iHeatMeterDao the iHeatMeterDao to set
	 */
	public void setiHeatMeterDao(IHeatMeterDao iHeatMeterDao) {
		this.iHeatMeterDao = iHeatMeterDao;
	}
	
}
